# agents/lygus_agent.py
from typing import List, Dict
import numpy as np
from game_engine.game_state import FactorDevelopment, GameWorld

class LygusAgent:
    """来古士智能体 - 每次轮回的反派"""
    
    def __init__(self, game_world: GameWorld):
        self.game_world = game_world
        self.strategies = ["divide_and_conquer", "focus_fire", "psychological_warfare"]
        self.current_strategy = "divide_and_conquer"
        self.taunt_cooldown = 0
        self.target_priority = self._init_target_priority()
    
    def _init_target_priority(self) -> List[str]:
        """初始化目标优先级"""
        return ["风堇", "阿格莱雅", "长夜月", "刻律德菈", "万敌", "其他"]
    
    def make_turn_decision(self, current_time: int) -> List[str]:
        """来古士的回合决策"""
        decisions = []
        
        # 每50个时间单位切换策略
        if current_time % 50 == 0:
            self.current_strategy = np.random.choice(self.strategies)
            decisions.append(f"切换策略为: {self.current_strategy}")
        
        # 执行策略
        if self.current_strategy == "divide_and_conquer":
            decisions.extend(self._divide_and_conquer())
        elif self.current_strategy == "focus_fire":
            decisions.extend(self._focus_fire())
        else:  # psychological_warfare
            decisions.extend(self._psychological_warfare())
        
        # 嘲讽技能
        if self.taunt_cooldown <= 0 and np.random.random() < 0.3:
            decisions.append("使用嘲讽技能")
            self.taunt_cooldown = 20
        
        if self.taunt_cooldown > 0:
            self.taunt_cooldown -= 1
        
        return decisions
    
    def _divide_and_conquer(self) -> List[str]:
        """分而治之策略"""
        decisions = []
        factors = self.game_world.factors
        
        # 攻击最分散的因子
        if len(factors) >= 2:
            isolated_factors = self._find_isolated_factors(factors)
            if isolated_factors:
                target = isolated_factors[0]
                decisions.append(f"攻击孤立因子: {target.name}")
        
        return decisions
    
    def _focus_fire(self) -> List[str]:
        """集火策略"""
        decisions = []
        factors = [f for f in self.game_world.factors if f.is_alive]
        
        if factors:
            # 按优先级选择目标
            for priority in self.target_priority:
                priority_targets = [f for f in factors if f.factor_type.value == priority]
                if priority_targets:
                    target = priority_targets[0]
                    decisions.append(f"集火目标: {target.name}")
                    break
        
        return decisions
    
    def _psychological_warfare(self) -> List[str]:
        """心理战策略"""
        decisions = []
        
        # 制造恐惧
        if np.random.random() < 0.4:
            decisions.append("施展恐惧领域")
        
        # 挑拨离间
        if np.random.random() < 0.25:
            decisions.append("使用挑拨技能")
        
        return decisions
    
    def _find_isolated_factors(self, factors: List[FactorDevelopment]) -> List[FactorDevelopment]:
        """寻找孤立的因子"""
        isolated = []
        for factor in factors:
            if not factor.is_alive:
                continue
                
            nearby_allies = 0
            for other in factors:
                if (other != factor and other.is_alive and
                    self._calculate_distance(factor.current_location, other.current_location) < 15.0):
                    nearby_allies += 1
            
            if nearby_allies == 0:  # 没有附近队友
                isolated.append(factor)
        
        return isolated
    
    def _calculate_distance(self, pos1: Tuple[float, float], pos2: Tuple[float, float]) -> float:
        """计算两点距离"""
        return np.sqrt((pos1[0] - pos2[0])**2 + (pos1[1] - pos2[1])**2)